package com.gxuwz.medical.domain.medical;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;

/**
 * 医疗机构管理类
 * @author 演示
 *
 */
public class Medical {
	/**
	 * 医疗机构编号
	 */
	private String medicalid;
	/**
	 * 医疗机构名称
	 */
	private String medicalname;
	public String getMedicalid() {
		return medicalid;
	}
	public void setMedicalid(String medicalid) {
		this.medicalid = medicalid;
	}
	public String getMedicalname() {
		return medicalname;
	}
	public void setMedicalname(String medicalname) {
		this.medicalname = medicalname;
	}
	
	public Medical() {

	}
	public Medical(String medicalid, String medicalname) {
		super();
		this.medicalid = medicalid;
		this.medicalname = medicalname;
		
	}
	public Medical(String medicalid)throws Exception {
		super();
		this.medicalid = medicalid;
		loadDB(medicalid);
	}
	/**
	 * 添加医疗机构
	 * @param medicalid
	 * @param medicalname
	 */
	public void addMed(String medicalid,String medicalname)throws Exception{
		Connection conn=null;
		try{
			//初始化属性
			this.medicalid=medicalid;
			this.medicalname=medicalname;
		   //保存导数据库
			conn=DbUtil.getConn();
			conn.setAutoCommit(false);
			//调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		}catch(Exception e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
		
	}
	/**
	 * 插入记录
	 * @param connection
	 * @throws SQLException
	 */
	private void saveToDB(Connection connection)throws SQLException{
		PreparedStatement pstmt=null;
		try{

			StringBuffer sqlBuff=new StringBuffer();
			sqlBuff.append("insert into t_medical(medicalid,medicalname)");
			sqlBuff.append("values(?,?)");
			String sql=sqlBuff.toString();
			pstmt=connection.prepareStatement(sql);
			//依次设置动态参数的值，注意顺序
			int index=1;
			pstmt.setString(index++, this.medicalid);
			pstmt.setString(index++, this.medicalname);
			//以executeUpdate()执行SQL
			pstmt.executeUpdate();
		}catch(SQLException e){
			throw e;
		}finally{
			//只关闭PrepareStatement资源
			if(pstmt!=null){
				pstmt.close();
			}
		}
	}
	public void delMed(String medicalid)throws Exception{
		Connection conn=null;
		   this.medicalid =medicalid;
		   try{
			   conn =DbUtil.getConn();
			   //1：开启手动提交事务
			   conn.setAutoCommit(false);
			   //2:删除农合机构信息
			   deleteFromDB(conn);
			  
			   conn.commit();
		   }catch(SQLException e){
			   conn.rollback();
			   throw e;
		   }finally{
			   DbUtil.close(conn);
		   }
		
	}
	private void deleteFromDB(Connection conn)throws SQLException{

		  PreparedStatement pstmt=null;
		  try{
			  StringBuffer sqlBuff=new StringBuffer("delete from t_medical where medicalid=?");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			  pstmt.setString(1, this.medicalid);

			  pstmt.executeUpdate(); 
		  }catch(SQLException e){
			  throw new SQLException("Failed to delete record from table !", e);
		  }finally{
			  DbUtil.close(pstmt);
		  }
	  
	   
	}
	public void editMed(String medicalname)throws Exception{
		Connection conn=null;
		this.medicalname =medicalname;
		
		try{
			conn =DbUtil.getConn();
			conn.setAutoCommit(false);
		    //保存到数据库
			editToDB(conn);
			conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	private void editToDB(Connection conn)throws SQLException{

		  PreparedStatement pstmt=null;
		  try{
			  StringBuffer sqlBuff=new StringBuffer("update t_medical t set t.medicalname=? where t.medicalid=?");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			  pstmt.setString(1, this.medicalname);
			  pstmt.setString(2, this.medicalid);
			  pstmt.executeUpdate(); 
		  }catch(SQLException e){
			  throw new SQLException("Failed to update t_medical !", e);
		  }finally{
			  DbUtil.close(pstmt);
		  } 
     }
	
	
	/**
	 * 加载农合机构信息
	 * @param areaid
	 * @throws SQLException
	 */
	private void loadDB(String medicalid)throws SQLException{
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DbUtil.getConn();
			pstmt=conn.prepareStatement("select * from t_medical where medicalid=?");
			int index=1;
			pstmt.setString(index, this.medicalid);
			rs=pstmt.executeQuery();
			if(rs.next()){
				this.medicalid=rs.getString("medicalid");
				this.medicalname=rs.getString("medicalname");
			}
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
	}
	
}
